function reset_fields() {
  $.ajax({
    async: false,
    timeout: 3, 
    url: '/accounts/api/user_info',
    type: 'GET',
    success: function (msg, status) {
      user_info = eval(msg)[0];
      $("#settings form #email").val(user_info.email).addClass('unchanged'); 
      $("#settings form #bio textarea").val(user_info.bio).addClass('unchanged');
      $("#settings form #avatar img").attr('src', user_info.avatar).height(128).width(128).attr('border', '0');
      $('#settings form #new-image').val('');
    },
    error: function (req, msg, status) {
      alert('sorry, failed to load user settings!');
    }   
  });
}

$(function () {
  $("#settings #email").change(function () {$(this).removeClass('unchanged');});
  $("#settings #bio > textarea").change(function () {$(this).removeClass('unchanged');});
  $('#settings form #submit').click(function () {
    var unchanged = $('#settings form #email').hasClass('unchanged') && $('#settings form #bio textarea').hasClass('unchanged') && $('#settings form #new-image').val() == "";
    if (!unchanged) {
      $('#settings form').submit();
      return true;
    } else {
      return false;
    }
  });
  $("#settings form #reset").click(reset_fields);
  $("#settings iframe").load(reset_fields);
});
